﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Core;
using Core.UIItems;
using Core.UIItems.ListBoxItems;
using Core.UIItems.WindowItems;


namespace UITestSuite
{
    [TestFixture]
    public class LogonDialogTests
    {
        private Core.Application applicationUnderTest;
        private WPFWindow mainWindow;
        private WPFWindow loginDialogWindow;
        private Button signInButton;
        private TextBox loginNameTextBox;
        private TextBox passwordTextBox;
        private Label statusLabel;

        /// <summary>
        /// Before each test, launch the app, get the main window, 
        /// click the login button, and find the controls
        /// </summary>
        [SetUp]
        public void Setup()
        {
            applicationUnderTest = Application.Launch("ForumsClient.exe");
            mainWindow = (WPFWindow)applicationUnderTest.GetWindow("Microsoft Forums Client - July 08 CTP", Core.Factory.InitializeOption.NoCache);
            Assert.IsNotNull(mainWindow);

            loginDialogWindow = (WPFWindow)mainWindow.ModalWindow("Logon", Core.Factory.InitializeOption.NoCache);
            Assert.IsNotNull(loginDialogWindow);

            statusLabel = loginDialogWindow.Get<Label>("StatusLabel");
            Assert.IsNotNull(statusLabel);
            Assert.IsFalse(statusLabel.Visible, "Status label is visible when the dialog comes up");

            signInButton = loginDialogWindow.Get<Button>("SignInButton");
            Assert.IsNotNull(signInButton);

            loginNameTextBox = loginDialogWindow.Get<TextBox>("LoginNameTextBox");
            Assert.IsNotNull(loginNameTextBox);

            passwordTextBox = loginDialogWindow.Get<TextBox>("PassWordTextBox");
            Assert.IsNotNull(passwordTextBox);
        }

        /// <summary>
        /// If the test fails, it might leave the app running so we check and 
        /// shut it down (if necessary)in the teardown 
        /// </summary>
        [TearDown]
        public void TearDown()
        {
            if (applicationUnderTest.HasExited == false)
            {
                applicationUnderTest.Kill();
            }
        }

        /// <summary>
        /// Click the Sign In button with no data in the
        /// sign in name and password fields and verifies that an
        /// error message is displayed in the login box
        /// </summary>
        [Test]
        public void NoDataEnteredTest()
        {
            signInButton.Click();
            Assert.IsTrue(statusLabel.Visible);
            loginDialogWindow.Close();
            mainWindow.Close();
        }

        /// <summary>
        /// Enters valid Sign In Name with an invalid password
        /// and clicks the Sign In button. This should result in an
        /// error message being displayed in the login box
        /// </summary>
        [Test]
        public void InvalidPasswordTest()
        {
            loginNameTextBox.Text = "test123456@test123456.com";
            // Typo in the password - last character is a 5 instead of 6
            passwordTextBox.Text = "123455";
            signInButton.Click();
            Assert.IsTrue(statusLabel.Visible);
            loginDialogWindow.Close();
            mainWindow.Close();
        }

        /// <summary>
        /// Enters a Sign In Name that contains a typo. The password is correct
        /// for the INTENDED signin name.
        /// Clicking the Sign In button should result in an
        /// error message being displayed in the login box
        /// </summary>
        [Test]
        public void InvalidSignInNameTest()
        {
            // Typo in user name tets instead of test in the first 4 characters
            loginNameTextBox.Text = "tets123456@test123456.com";
            passwordTextBox.Text = "123456";
            signInButton.Click();
            Assert.IsTrue(statusLabel.Visible);
            loginDialogWindow.Close();
            mainWindow.Close();
        }

        /// <summary>
        /// Sigin in with valid credentials.
        /// This should close the Login dialog so 
        /// we should not be able to find it with the 
        /// White test framework. White will throw an exception 
        /// which we can ignore.
        /// </summary>
        [Test]
        [ExpectedException(typeof(Core.UIItems.UIActionException))]
        public void ValidUserTest()
        {
            loginNameTextBox.Text="test123456@test123456.com";
            passwordTextBox.Text = "123456";
            signInButton.Click();

            // If the login dialog closes successfully (like it should), this will throw an exception
            loginDialogWindow = (WPFWindow)mainWindow.ModalWindow("Logon", Core.Factory.InitializeOption.NoCache);
            mainWindow.Close();
        }

    }
}
